﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Bootstrap.MVC.Common;
using Bootstrap.MVC.Infrastructure.Html;
using Bootstrap.MVC.Utils;

namespace Bootstrap.MVC.Widgets
{
    public class ButtonHtmlGenerator : HtmlGeneratorBase<IButtonModel>
    {
        public ButtonHtmlGenerator(IButtonModel model) : base(model) { }


        public HtmlTagBuilder  AsHtmlBuilder()
        {
             var btnTag = new HtmlTagBuilder("button")
                                    .Attr(new { @class = "btn", type = "button" })
                                    .Text(Model.Text);

            if(Model.Skin != Skins.Default)
                btnTag.AddClass("btn-" + Model.Skin.GetStringValue());

            if (Model.Block)
                btnTag.AddClass("btn-block");

            if (Model.Size != ButtonSize.Normal)
                btnTag.AddClass(Model.Size.GetStringValue());

            if (Model.Disabled)
                btnTag.AddClass("disabled");

            if (!string.IsNullOrEmpty(Model.Icon))
            {
                var iconTag = new HtmlTagBuilder("i").AddClass(Model.Icon);
                
                if (Model.IconColor == IconColors.White)
                    iconTag.AddClass("icon-white"); 

                btnTag.Append(iconTag);
            }

            // it migth be moved to the helper method in base class
            foreach(var e in Model.Events.EventBag)
            {
                btnTag.Attr(e.EventName , "{0}()".FormatWith(e.Handler.Replace("()","")));
            }


            return btnTag;
        }

        public override string Render()
        {
            return AsHtmlBuilder().Render(Model.IconPosition == LeftOrRight.Right );
        }
    }
}
